import { Component, OnInit } from '@angular/core';
import { RepairRequest, RepairStatus, RepairPriority } from '../../models/device.model';
import { RepairRequestService } from '../../services/repair-request.service';

@Component({
  selector: 'app-repair-request-list',
  templateUrl: './repair-request-list.component.html',
  styleUrls: ['./repair-request-list.component.scss']
})
export class RepairRequestListComponent implements OnInit {
  repairRequests: RepairRequest[] = [];
  loading = false;

  constructor(private repairRequestService: RepairRequestService) { }

  ngOnInit(): void {
    this.loadRepairRequests();
  }

  loadRepairRequests(): void {
    this.loading = true;
    this.repairRequestService.getAllRepairRequests().subscribe({
      next: (requests) => {
        this.repairRequests = requests;
        this.loading = false;
      },
      error: (error) => {
        console.error('Error loading repair requests:', error);
        this.loading = false;
      }
    });
  }

  getStatusText(status: RepairStatus): string {
    switch (status) {
      case RepairStatus.Pending: return '待审批';
      case RepairStatus.Approved: return '已审批';
      case RepairStatus.InProgress: return '进行中';
      case RepairStatus.Completed: return '已完成';
      case RepairStatus.Rejected: return '已拒绝';
      default: return '未知';
    }
  }

  getStatusClass(status: RepairStatus): string {
    switch (status) {
      case RepairStatus.Pending: return 'warning';
      case RepairStatus.Approved: return 'success';
      case RepairStatus.InProgress: return 'info';
      case RepairStatus.Completed: return 'success';
      case RepairStatus.Rejected: return 'danger';
      default: return 'secondary';
    }
  }

  getPriorityText(priority: RepairPriority): string {
    switch (priority) {
      case RepairPriority.Low: return '低';
      case RepairPriority.Medium: return '中';
      case RepairPriority.High: return '高';
      case RepairPriority.Critical: return '紧急';
      default: return '未知';
    }
  }

  getPriorityClass(priority: RepairPriority): string {
    switch (priority) {
      case RepairPriority.Low: return 'secondary';
      case RepairPriority.Medium: return 'info';
      case RepairPriority.High: return 'warning';
      case RepairPriority.Critical: return 'danger';
      default: return 'secondary';
    }
  }

  approveRequest(id: number): void {
    this.repairRequestService.approveRepairRequest(id).subscribe({
      next: () => {
        this.loadRepairRequests();
      },
      error: (error) => {
        console.error('Error approving request:', error);
      }
    });
  }

  rejectRequest(id: number): void {
    const reason = prompt('请输入拒绝原因:');
    if (reason) {
      this.repairRequestService.rejectRepairRequest(id, reason).subscribe({
        next: () => {
          this.loadRepairRequests();
        },
        error: (error) => {
          console.error('Error rejecting request:', error);
        }
      });
    }
  }
} 